package pl.gwt.client.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import pl.gwt.client.CRMEntryPoint;
import pl.gwt.client.ClientLiveSearch;
import pl.gwt.client.ClientLiveSearchAsync;
import pl.gwt.client.gui.Clients.Images;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ImageBundle.Resource;

public class Projects extends Composite {

	
	public interface Images extends ImageBundle{
		@Resource("default_photo.jpg")
	    AbstractImagePrototype defaultPhoto(); 
	}
	
	private VerticalPanel panel = new VerticalPanel();
	private SuggestBox clientsTextBox;
	private Button searchClientButton = new Button("GO!");
		
	private final Images images;
	MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
	
	final ClientLiveSearchAsync clientLiveSearch = 
				(ClientLiveSearchAsync)GWT.create(ClientLiveSearch.class);
	
	public Projects(Images images) {
		ServiceDefTarget endPoint = (ServiceDefTarget) clientLiveSearch;
		endPoint.setServiceEntryPoint("pl.gwt.server.services");
		
		AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>(){

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Cannot get projects list because "+ caught.getMessage());
			}

			public void onSuccess(List<String> result) {
				// TODO Auto-generated method stub
				ArrayList<String> resultItems = (ArrayList<String>)result;
				for (Iterator<String> iter = resultItems.iterator(); iter
						.hasNext();) {
					oracle.add(iter.next());
				}
			}
		};
		clientLiveSearch.getCompletionItems(callback);
			
		searchClientButton.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				
				CRMEntryPoint.get().changeRightPanel(new RightPanelClientDetails(clientsTextBox.getText()));
				
			}
			
		});
		
		clientsTextBox = new SuggestBox(oracle);
		SimplePanel outer = new SimplePanel();
	    outer.setWidget(panel);

	    Label searchClientLabel = new Label("Search project");
	    panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    searchClientLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    panel.add(searchClientLabel);
	    panel.add(clientsTextBox);
	    panel.add(searchClientButton);
	    
	    this.images = images;
	    
		initWidget(outer);
	}
}
